package com.atguigu.springcloud.filter;

import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

/**
 * @auther zzyy
 * @create 2020-02-21 11:42
 */
@Configuration
public class GateWayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
//        语法
//        routes.route("path_route_atguigu",
//                r -> r.path("/guonei")
//                        .uri("http://news.baidu.com/guonei")).build();// 转换成能看懂的样子
        // 这里是创建了function的匿名实现类
        routes.route("path_route_atguigu",
                new Function<PredicateSpec, Route.AsyncBuilder>() {
                    @Override
                    public Route.AsyncBuilder apply(PredicateSpec r) {
                        return r.path("/guonei")
                                .uri("http://news.baidu.com/guonei");
                    }
                }).build();
        // 这里的路由的意思就是   路径为这个/guonei,让后跳转到http://news.baidu.com/guonei这个地址
        return routes.build();
    }
}
